using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Drawing;

using TreeSharp;

using Styx;
using Styx.Logic;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Helpers;

namespace FelMaster.Classes.Mage
{
    class IceFrost : TalentBase
    {
        public override string Name
        {
            get { return "Frost V1.01"; }
        }

        public override string KeySpell
        {
            get { return "Summon Water Elemental"; }
        }

        public List<WoWUnit> AOECHK(WoWPoint chkloc, double radius)
        {
            List<WoWUnit> hostile = ObjectManager.GetObjectsOfType<WoWUnit>(true, false);
            List<WoWUnit> avoid = new List<WoWUnit>();
            var MaxDistance2 = radius * radius;
            try
            {
                hostile = hostile.Where(x =>
                    x.IsAlive &&
                    (x.IsTargetingMeOrPet || x.IsTargetingMyPartyMember || x.IsTargetingMyRaidMember || x.IsTargetingPet) &&
                    !x.IsCritter &&
                    !x.IsPet &&
                    x.Location.Distance2DSqr(chkloc) < MaxDistance2
                ).ToList();
                avoid = hostile.Where(x =>
                    //check for controlled units, like sheep etc
                SimCraftBase.Instance.UnitIsControlled(x, true)
                ).ToList();
                if (avoid != null && avoid.Count > 0)
                {
                    hostile = null;
                    hostile.Add(Me.CurrentTarget);
                }
            }
            catch { }
            return hostile;
        }

        public override Composite SingleRotation
        {
            get {
                return new PrioritySelector(
                    new Decorator(x => SC.PlayerIsChanneling, new TreeSharp.Action(a => SC.Log("Channeling"))),
                    SC.ChannelSpell("Evocation", a => Me.ManaPercent < 40, "Evocate"),
                    SC.UseBagItem("Mana Gem", a => Me.ManaPercent < 80, "Using Mana Gem"),
                    SC.CastSpell("Cold Snap", a => SC.SpellCooldown("Deep Freeze") > 15 && SC.SpellCooldown("Flame Orb") > 30 && SC.SpellCooldown("Icy Veins") > 30, "Cold Snap"),
                    SC.CastSpell("Flame Orb", a => true, "Flame Orb"),
                    SC.CastSpell("Mirror Image", a => ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)), "Mirror Image"),
                    SC.CastSpell("Icy Veins", a => ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)) && !(SC.PlayerHasBuff("Icy Veins") || SC.PlayerHasBuff("Time Warp") || SC.PlayerHasBuff("Bloodlust") || SC.PlayerHasBuff("Heroism") || SC.PlayerHasBuff("Ancient Hysteria")), "Icy Veins"),
                    SC.UseEngineerGloves(a => true, "Engineer's glove enchant"),
                    SC.CastSpell("Berserking", a => ((Me.CurrentTarget.Level == 87 && Me.CurrentTarget.Elite) || (Me.CurrentTarget.Level == 88)), "Berserking"),
                    new Decorator(
                        ret => !SC.PlayerIsChanneling && !SC.PlayerHasBuff("Fingers of Frost") && SC.CanCast("Freeze"),
                        new Sequence(
                            new TreeSharp.Action(ret => SC.CastSpell("Freeze")),
                            new TreeSharp.Action(ret => Styx.Logic.Combat.LegacySpellManager.ClickRemoteLocation(WoWMathHelper.CalculatePointFrom(Me.Location, Me.CurrentTarget.Location,Math.Max(5, Me.CombatReach+1.33333f + Me.CurrentTarget.CombatReach) - 2f)))
                        )
                    ),
                    SC.CastConicSpell("Cone of Cold", 11f, 33f, a => AOECHK(Me.CurrentTarget.Location, 8).Count >= 5, "Cone of Cold"),
                    new Decorator(
                        ret => Me.GotTarget && !Me.IsMoving && AOECHK(Me.CurrentTarget.Location, 8).Count >= 7 && !SC.PlayerIsChanneling,
                        new Sequence(
                            new TreeSharp.Action(ret => SC.CastSpell("Blizzard")),
                            new TreeSharp.Action(ret => Styx.Logic.Combat.LegacySpellManager.ClickRemoteLocation(Me.CurrentTarget.Location))
                        )
                    ),
                    SC.CastSpell("Deep Freeze", a => true, "Deep Freeze"),
                    SC.CastSpell("Frostfire Bolt", a => SC.PlayerHasBuff("Brain Freeze") && (SC.PlayerHasBuff("Fingers of Frost") || SC.SpellCooldown("Freeze") > 6), "Frostfire Bolt"),
                    SC.CastSpell("Ice Lance", a => SC.PlayerCountBuff("Fingers of Frost") == 2, "Ice Lance"),
                    SC.CastSpell("Ice Lance", a => SC.SpellCooldown("Freeze") < 1 && SC.PlayerHasBuff("Fingers of Frost"), "Ice Lance"),
                    SC.CastSpell("Ice Lance", a => SC.PlayerBuffTimeLeft("Fingers of Frost") < 2 && SC.PlayerHasBuff("Fingers of Frost"), "Ice Lance"),
                    SC.CastSpell("Fire Blast", a => Me.IsMoving, "Fire Blast"),
                    SC.CastSpell("Ice Lance", a => Me.IsMoving, "Ice Lance"),
                    SC.CastSpell("Frostbolt", a => true, "Frostbolt")
                );
            }
        }

        public override Composite Buffs
        {
            get
            {
                return new PrioritySelector(
                    SC.CastUniqueSpell("Summon Water Elemental", a => (Pet == null || Pet.Dead), "Summon Water Elemental"),
                    SC.CastBuff("Molten Armor", a => Me.ManaPercent > 80 && !SC.PlayerHasBuff("Molten Armor"), "Dont Need Mana - Switching To Molten Armor"),
                    SC.CastBuff("Mage Armor", a => Me.ManaPercent < 10 && !SC.PlayerHasBuff("Mage Armor"), "Need Mana - Switching To Mage Armor"),
                    SC.CastBuff("Mage Armor", a => !SC.PlayerHasBuff("Mage Armor") && !SC.PlayerHasBuff("Molten Armor"), "Mage Armor")
                );
            }
        }

        public override Composite PVPRotation
        {
            get { throw new NotImplementedException(); }
        }

        public override Composite PVERotation
        {
            get
            {
                return SingleRotation;
            }
        }
    }
}